/**
 * 
 */
package org.crown.kings.dao;

import java.io.IOException;
import java.io.Reader;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;

/**
 * Creates a DBMapper which in turn will create a SqlMapClient given
 * the appropriate configuration file.
 * 
 * @author Nathan
 *
 */
public class DBMapper {
	
	private static DBMapper instance;
	private static Log logger = LogFactory.getLog(DBMapper.class);
	
	private SqlMapClient sqlMapper;
	
	private DBMapper() {}
	
	public static DBMapper instance()
	{
		if (instance == null)
		{
			synchronized (new Object())
			{
				try
				{
					instance = new DBMapper();
					Reader reader = Resources.getResourceAsReader("sqlMapper.xml");
					instance.sqlMapper = SqlMapClientBuilder.buildSqlMapClient(reader);
				}
				catch (IOException ioe)
				{
					logger.error("Error while trying to create the SQL mapper for iBATIS", ioe);
				}
			}
		}
		
		return instance;
	}
	
	public SqlMapClient getSqlMapper()
	{
		return sqlMapper;
	}

}
